<template>
	<view>
		<view class="usermain" style="padding-bottom: 170rpx;">
			<view class="usermain-item ">
				<view>头像</view>
				<view>
					<!-- #ifdef MP-WEIXIN -->
					<button open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<image style="width: 100rpx;height: 100rpx;border-radius: 50%" :src="avatar"></image>
					</button>
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<image :src="avatar" style="width: 100rpx;height: 100rpx;border-radius: 100rpx;"
						@click="uploadImg()"></image>
					<!-- #endif -->
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>用户名</view>
				<view>
					<view class="cu-form-group">
						<input type="nickname" v-model="userName" placeholder="请输入用户名" />
					</view>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>年龄</view>
				<view>
					<view class="cu-form-group">
						<input v-model="age" />
					</view>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>手机</view>
				<view>
					<view class="cu-form-group">
						<input v-model="phone" placeholder="请输入联系电话" :disabled="true" />
					</view>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>性别</view>
				<view>
					<view class="cu-form-group">
						<u-radio-group v-model="sex">
							<u-radio shape="circle" :name="1">男</u-radio>
							<u-radio shape="circle" active-color="red" :name="2">女</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
		</view>
		<view class="footer-btn">
			<view class="usermain-btn" @click="messagebtn()">保存</view>
		</view>
		<!-- 时间筛选 -->
		<u-picker v-model="startShow" mode="time" :params="params" @confirm="getStarttime()"></u-picker>

		<u-picker v-model="endShow" mode="time" :params="paramss" @confirm="getEndtime()"></u-picker>
	</view>
</template>

<script>
	import configdata from '../../common/config.js';
	export default {
		data() {
			return {
				phone: '',
				avatar: '../../static/logo.png',
				userName: '',
				nickName: '',
				userId: '',
				realName: '',
				weChatId: "",
				password: '',
				platform: '',
				createTime: '',
				money: '',
				jiFen: '',
				status: '',
				zhiFuBao: '',
				zhiFuBaoName: '',
				sex: 1,
				age: 0,
				// value: '',
				// Imgo: [], //主页轮播图
				// detailsImg:'',
				// zizhi: [], //我的资质
				// certificationImg:'',
				// details:'',//介绍
				form: {
					address: '',
					shopPhone: '',
					shopImg: '',
					shopName: '',
					addressImg: '',
					startTime: '',
					endTime: '',
					shopType: '',
					latitude: '',
					longitude: ''
				},
				startShow: false,
				endShow: false,
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				paramss: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				addressImg: []
			};
		},
		onLoad(e) {
			this.getUserInfo()
			// this.avatar = uni.getStorageSync('avatar')
		},
		methods: {
			//微信填写能力获取头像
			onChooseAvatar(e) {
				console.log(e.detail.avatarUrl)
				let that = this;
				let token = uni.getStorageSync('token');
				uni.showLoading({
					title: '上传中...'
				});
				uni.uploadFile({
					// url: config.APIHOST1 + '/alioss/upload', //仅为示例，非真实的接口地址
					url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload', //仅为示例，非真实的接口地址
					filePath: e.detail.avatarUrl,
					// filePath: res.tempFilePaths[0],
					header: {
						token: token
					},
					name: 'file',
					success: uploadFileRes => {
						let url = JSON.parse(uploadFileRes.data).data;
						that.$Request.postJson(
							'/app/user/updateUserImageUrl?avatar=' + url).then(
							res => {
								uni.hideLoading();
								if (res.code === 0) {
									that.$queue.showToast(
										"更新成功");
									that.getUserInfo();
								}
							});
					}
				});
			},
			bindTimeChange: function(e) {
				console.log(e)
				this.form.startTime = e.target.value
			},
			bindTimeChanges: function(e) {
				console.log(e)
				this.form.endTime = e.target.value
			},
			removeImg(index) {
				console.log(index)
				this.addressImg.splice(index, 1)
			},
			getStarttime(e) {
				this.form.startTime = e.hour + ':' + e.minute

			},
			getEndtime(e) {
				this.form.endTime = e.hour + ':' + e.minute
			},
			// 店铺地址
			bindOpen(e) {
				console.log(e)
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.form.address = res.address
						that.form.latitude = res.latitude
						that.form.longitude = res.longitude
					}
				});
			},
			goMyAddress() {
				uni.navigateTo({
					url: '../jifen/myaddress'
				});
			},
			uploadImg() {
				let token = uni.getStorageSync('token')

				if (!token) {
					this.goLoginInfo();
					return;
				}
				let that = this;
				var url = null;
				uni.showActionSheet({
					// itemList按钮的文字接受的是数组
					itemList: ["查看头像", "从相册选择图片"],
					success(e) {
						var index = e.tapIndex
						if (index === 0) {
							// 用户点击了预览当前图片
							// 可以自己实现当前头像链接的读取
							let url = that.avatar;
							let arr = []
							arr.push(url)
							uni.previewImage({
								// 预览功能图片也必须是数组的
								urls: arr
							})
						} else if (index === 1) {
							uni.chooseImage({
								count: 1, //默认9
								sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
								sourceType: ['album'], //从相册选择
								success: function(res) {
									uni.showLoading({
										title: '上传中...'
									});
									let token = uni.getStorageSync('token');
									let userId = uni.getStorageSync('userId');
									uni.uploadFile({
										// url: that.config("APIHOST1") +
										// 	'/alioss/upload', //真实的接口地址
										url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload',
										filePath: res.tempFilePaths[0],
										header: {
											token: token
										},
										name: 'file',
										success: uploadFileRes => {
											url = JSON.parse(uploadFileRes.data);
											that.avatar = url.data
											uni.hideLoading();
										}
									});
								}
							});
						}
					}
				})
			},
			// 图片上传
			addImage() {
				let that = this
				uni.chooseImage({
					count: 6,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: that.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									uni.hideLoading();
									if (that.addressImg.length < 6) {
										that.addressImg.push(JSON.parse(uploadFileRes.data)
											.data)
									}
								}
							});
						}
					}
				})
			},
			addImages(e) {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < 1; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址https://xxpwadmin.xianmxkj.com/sqx_fast/alioss/upload
								url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 4) {
										that.form.shopImg = JSON.parse(uploadFileRes.data).data
									}

									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
			getUserInfo() {
				let userId = uni.getStorageSync('userId')
				this.$Request.get("/app/user/selectUserById").then(res => {
					if (res.code == 0) {
						this.$queue.setData('avatar', res.data.avatar);
						this.$queue.setData('userId', res.data.userId);
						this.$queue.setData('userName', res.data.userName);
						this.$queue.setData('phone', res.data.phone);
						this.$queue.setData('age', res.data.age);
						this.sex = res.data.sex
						this.age = res.data.age
						// this.phone = res.data.shopPhone;
						this.avatar = res.data.avatar;
						this.phone = res.data.phone;
						this.userName = res.data.userName;
						this.form.address = res.data.address
						this.form.shopPhone = res.data.shopPhone
						this.form.shopImg = res.data.shopImg
						this.form.shopName = res.data.shopName
						if (res.data.addressImg) {
							this.addressImg = res.data.addressImg.split(',')
						}

						this.form.startTime = res.data.startTime
						this.form.endTime = res.data.endTime
						this.form.shopType = res.data.shopType
						this.form.latitude = res.data.latitude
						this.form.longitude = res.data.longitude
						if (this.userName == null) {
							this.userName = res.data.nickName;
						} else {
							this.userName = res.data.userName;
						}
					}
					uni.hideLoading();
				});


			},
			// 保存
			messagebtn() {
				// this.form.addressImg = this.addressImg
				// this.form.addressImg = this.form.addressImg.toString();
				// if (!this.form.shopName) {
				// 	uni.showToast({
				// 		title: '请填写店铺名称',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.shopType) {
				// 	uni.showToast({
				// 		title: '请填写店铺类型',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.startTime) {
				// 	uni.showToast({
				// 		title: '请填写开店时间',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.endTime) {
				// 	uni.showToast({
				// 		title: '请填写闭店时间',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.shopPhone) {
				// 	uni.showToast({
				// 		title: '请填写店铺电话',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.address) {
				// 	uni.showToast({
				// 		title: '请填写店铺地址',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.shopImg) {
				// 	uni.showToast({
				// 		title: '请上传店铺logo',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				// if (!this.form.addressImg) {
				// 	uni.showToast({
				// 		title: '请上传店铺主页图',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// 	return
				// }
				if (!this.userName) {
					// this.$queue.showToast('用户名不能为空');
					uni.showToast({
						title: "用户名不能为空",
						icon: "none"
					})
				} else if (!this.phone) {
					// this.$queue.showToast('用户名不能为空');
					uni.showToast({
						title: "联系电话不能为空",
						icon: "none"
					})
				} else {
					uni.showModal({
						title: '温馨提示',
						content: '确定保存信息',
						success: e => {
							if (e.confirm) {
								this.$Request.postJson("/app/user/updateUser", {
									userName: this.userName,
									avatar: this.avatar,
									phone: this.phone,
									sex: this.sex,
									age: this.age,
									details: this.details,
									detailsImg: this.detailsImg,
									certificationImg: this.certificationImg,
									address: this.form.address,
									shopPhone: this.form.shopPhone,
									shopImg: this.form.shopImg,
									shopName: this.form.shopName,
									addressImg: this.form.addressImg,
									startTime: this.form.startTime,
									endTime: this.form.endTime,
									shopType: this.form.shopType,
									latitude: this.form.latitude,
									longitude: this.form.longitude
								}).then(res => {
									if (res.code === 0) {
										uni.showToast({
											title: '保存成功',
											icon: "none"
										})
										setTimeout(function() {
											uni.navigateBack()
										}, 1000)
									} else {
										uni.showToast({
											title: res.msg,
											icon: "none"
										})
									}
								})
							}
						}
					});
				}
			}
		},
		// userphone(){
		// 	uni.navigateTo({
		// 		url:'/pages/my/userphone'
		// 	})
		// }

	};
</script>

<style>
	page {
		/* background: #1c1b20; */
	}

	button::after {
		border: none;
		background-color: none;
	}

	button {
		position: relative;
		display: block;
		margin-left: auto;
		margin-right: auto;
		padding-left: 0px;
		padding-right: 0px;
		box-sizing: border-box;
		text-decoration: none;
		line-height: 1.35;
		overflow: hidden;
		color: #666666;
		/* background-color: #fff; */
		background-color: rgba(255, 255, 255, 0) !important;
		width: 100%;
		height: 100%;
	}

	.bg {
		background: #FFFFFF;
	}

	.usermain {
		background: #ffffff;
		/* color: #fff; */
	}

	.usermain-item {
		display: flex;
		align-items: center;
		margin: 0 40rpx;
		padding: 10rpx 0;
		justify-content: space-between;
		border-bottom: 1rpx solid #e5e5e5;
		/* border-bottom: 2rpx solid #f2f2f2; */
	}

	.usermain-item.item-padding {
		/* padding: 0; */
	}

	.cu-form-group {
		padding: 0;
		background: #ffffff;
		text-align: right;
	}

	.cu-form-group input {
		background: #ffffff;
		font-size: 28rpx;
		/* color: #fff; */

	}

	.footer-btn {
		position: fixed;
		bottom: 0;
		right: 0;
		left: 0;
		background: #ffffff;
		padding: 30rpx 0;
		z-index: 9;
	}

	.footer-btn .usermain-btn {
		color: #FFFFFF;
		background: #557EFD;
		text-align: center;
		width: 450rpx;
		height: 80rpx;
		font-size: 28rpx;
		line-height: 80rpx;
		margin: 0 auto;
		border-radius: 40rpx;
	}
</style>
